package com.kinsin.kinsinutil

import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.util.*

class TimeUtilsTest {
    companion object {
        // 测试用固定时间戳（北京时间 2023-01-01 08:00:00）
        const val TEST_TIMESTAMP_PRECISE = 1672531200000L
        const val TEST_TIMESTAMP_DAY = 1672502400000L
    }

    @Before
    fun setup() {
        // 确保测试环境时区不影响结果
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"))
    }

    // region 时间戳获取测试
    @Test
    fun testGetCurrentTimestamp() {
        val expected = System.currentTimeMillis()
        assertTrue("时间戳差异过大", abs(TimeUtils.getCurrentTimestamp() - expected) <= 1000)
    }

    @Test
    fun testGetCurrentTimestampInSeconds() {
        val expected = System.currentTimeMillis() / 1000
        assertTrue("时间戳差异过大", abs(TimeUtils.getCurrentTimestampInSeconds() - expected) <= 1)
    }
    // endregion

    // region 时间格式化测试
    @Test
    fun testFormatTimestamp() {
        // 验证东八区时间
        assertEquals("2023-01-01", TimeUtils.formatTimestamp(TEST_TIMESTAMP_PRECISE, TimeUtils.FORMAT_YYYY_MM_DD))
        assertEquals("2023-01-01 08:00:00", TimeUtils.formatTimestamp(TEST_TIMESTAMP_PRECISE))
        assertEquals("08:00:00", TimeUtils.formatTimestamp(TEST_TIMESTAMP_PRECISE, TimeUtils.FORMAT_HH_MM_SS))
        assertEquals("", TimeUtils.formatTimestamp(TEST_TIMESTAMP_PRECISE, "invalid-format"))
    }

    @Test
    fun testParseFormattedTime() {
        assertEquals(TEST_TIMESTAMP_DAY, TimeUtils.parseFormattedTime("2023-01-01", TimeUtils.FORMAT_YYYY_MM_DD))
        assertEquals(TEST_TIMESTAMP_PRECISE, TimeUtils.parseFormattedTime("2023-01-01 08:00:00"))
        assertEquals(0L, TimeUtils.parseFormattedTime("invalid-date"))
    }

    @Test
    fun testGetCurrentFormattedTime() {
        val regex = """\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}""".toRegex()
        assert(TimeUtils.getCurrentFormattedTime().matches(regex))
    }
    // endregion

    // region 时间差计算测试
    @Test
    fun testTimeDifferences() {
        val start = TEST_TIMESTAMP_PRECISE
        val end = start + 2 * 60 * 60 * 1000 // +2小时

        assertEquals(2 * 60 * 60 * 1000L, TimeUtils.getTimeDifferenceInMillis(start, end))
        assertEquals(2 * 60 * 60L, TimeUtils.getTimeDifferenceInSeconds(start, end))
        assertEquals(120L, TimeUtils.getTimeDifferenceInMinutes(start, end))
        assertEquals(2L, TimeUtils.getTimeDifferenceInHours(start, end))
        assertEquals(0L, TimeUtils.getTimeDifferenceInDays(start, end))
    }

    @Test
    fun testCalculateTimeDifferenceInSeconds() {
        val diff = TimeUtils.calculateTimeDifferenceInSeconds(
            "2023-01-01 09:00:00",
            "2023-01-01 08:30:00",
            TimeUtils.FORMAT_YYYY_MM_DD_HH_MM_SS
        )
        assertEquals(1800, diff)
    }
    // endregion

    // region 持续时间格式化测试
    @Test
    fun testFormatDuration() {
        assertEquals("02时30分15秒", TimeUtils.formatDuration(2 * 3600 + 30 * 60 + 15))
        assertEquals("45分05秒", TimeUtils.formatDuration(45 * 60 + 5))
        assertEquals("30秒", TimeUtils.formatDuration(30))
        assertEquals("00秒", TimeUtils.formatDuration(0))
        assertEquals("无效时长", TimeUtils.formatDuration(-5))
    }

    @Test
    fun testDayBoundary() {
        assertEquals("23时59分59秒", TimeUtils.formatDuration(86399))
    }
    // endregion

    // region 时区验证测试
    @Test
    fun testTimezoneIndependence() {
        TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"))
        assertEquals("2023-01-01 08:00:00", TimeUtils.formatTimestamp(TEST_TIMESTAMP_PRECISE))
    }
    // endregion

    private fun abs(value: Long) = if (value < 0) -value else value
}